package com.xcm.controller;

import com.google.common.collect.Lists;
import com.xcm.config.CustomProperties;
import com.xcm.constant.BusinessConstant;
import com.xcm.constant.ResponseConstant;
import com.xcm.exception.ControllerException;
import com.xcm.model.JsonResponse;
import com.xcm.service.FileService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 上传Controller
 */
@Controller
@RequestMapping(value = "/file")
public class FileController {
	private static Logger logger = LoggerFactory.getLogger(FileController.class);
	@Autowired
	private FileService fileService;
	@Autowired
	private CustomProperties customProperties;

	/**
	 * apk文件上传
	 */
	@ResponseBody
	@RequestMapping(value = "/upload/apk", method = RequestMethod.POST)
	public Object uploadFile(@RequestParam("files") MultipartFile files) {
		if (files == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "上传文件不能为空");
		} else if (files.getSize() <= 0) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "文件内容不能为空");
		}
		String fileName = files.getOriginalFilename();
		if (StringUtils.isBlank(fileName)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "文件名不能为空");
		}
		if (!fileName.endsWith(BusinessConstant.FILE_SUFFIX_APK)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "文件格式不对！");
		}
		List msgList = Lists.newArrayList();
		try {
			Map<Object, Object> msgMap = fileService.saveApkFile(files);
			msgList.add(msgMap);
			return msgList;
		} catch (IOException e) {
			e.printStackTrace();
			logger.error("FileController uploadFile apk上传文件失败：" + e.getMessage());
			return new ControllerException("apk上传文件失败");
		}
	}

	/**
	 * 固件文件上传
	 */
	@ResponseBody
	@RequestMapping(value = "/upload/firmware", method = RequestMethod.POST)
	public Object uploadFirmwareFile(@RequestParam("files") MultipartFile files) {
		if (files == null) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "上传文件不能为空");
		} else if (files.getSize() <= 0) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "文件内容不能为空");
		}
		String fileName = files.getOriginalFilename();
		if (StringUtils.isBlank(fileName)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "文件名不能为空");
		}
		if (!fileName.endsWith(BusinessConstant.FILE_SUFFIX_ZIP)) {
			return new JsonResponse(ResponseConstant.CODE_FAIL, "文件格式不对！");
		}
		List<Map<Object, Object>> msgList = Lists.newArrayList();
		try {
			Map<Object, Object> msgMap = fileService.saveFirmwareFile(files);
			msgList.add(msgMap);
			return msgList;
		} catch (IOException e) {
			e.printStackTrace();
			logger.error("FileController uploadFirmwareFile 上传固件文件失败：" + e.getMessage());
			return new ControllerException("固件上传文件失败");
		}
	}
}
